//
//  ZPjiugonggeViewController.m
//  HCNewGoldFinger
//
//  Created by zjhcsoft on 16/6/15.
//  Copyright © 2016年 zjhcsoftios. All rights reserved.
//

#import "ZPjiugonggeViewController.h"
#import "ItemView.h"
#import "KSBLCollectionViewCell.h"
#import "HCGFReceiptView.h"
#import "NSBundle+AssociatedBundle.h"
#import "AppModuleModel.h"
#import "NetworkAddress.h"
#import <SDWebImage/SDWebImage.h>
#import "SVProgressHUD.h"
#import "iSeeNetworkRequest.h"

@interface ZPjiugonggeViewController ()<UICollectionViewDelegate,UICollectionViewDataSource>

@property(nonatomic,strong) NSMutableArray *dataList;

@property(nonatomic,strong)UICollectionView * collect;

@end

@implementation ZPjiugonggeViewController


- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    _dataList = [NSMutableArray array];
    [self getHomeEnterListMarket_main];
    [self createcollview];

}

-(void)createcollview{
    //创建一个layout布局类
    UICollectionViewFlowLayout * layout = [[UICollectionViewFlowLayout alloc]init];
    //设置布局方向为垂直流布局
    layout.scrollDirection = UICollectionViewScrollDirectionVertical;
    //设置每个item的大小为100*100
    layout.itemSize = CGSizeMake(kScreenWidth/4, 70);
    //创建collectionView 通过一个布局策略layout来创建
    _collect = [[UICollectionView alloc]initWithFrame:self.view.frame collectionViewLayout:layout];
    //代理设置
    _collect.delegate=self;
    _collect.dataSource=self;
    //注册item类型 这里使用系统的类型
    [_collect registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"cellid"];

    [self.view addSubview:_collect];
    UINib *nib =[UINib nibWithNibName:@"KSBLCollectionViewCell" bundle:[NSBundle bundleWithBundleName:@"NewMarketLib" podName:@"NewMarketLib"]];
    [_collect registerNib:nib forCellWithReuseIdentifier:@"rootcell"];
    _collect.backgroundColor =[UIColor whiteColor];
}



//这是正确的方法
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    KSBLCollectionViewCell * cell  = [collectionView dequeueReusableCellWithReuseIdentifier:@"rootcell" forIndexPath:indexPath];
    AppModuleModel *model =_dataList[indexPath.row];
    cell.itemview.titleLabel.text =model.name;
    cell.itemview.badgeLabel.hidden = YES;
    NSString * urlStr = [NSString stringWithFormat:@"%@%@",NMarketMainUrl,model.iosIcon];
    NSURL * url = [[NSURL alloc] initWithString:urlStr];
    [cell.itemview.imageView sd_setImageWithURL:url];
    

    
    return cell;
}


//选择了某个cell
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    AppModuleModel *model =_dataList[indexPath.row];

    [self  appModuleModeAction:model];

}

//返回分区个数
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return 1;
}
//返回每个分区的item个数
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return _dataList.count;
}
//每个section中不同的行之间的行间距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
{
    return 0;
}



//定义每个UICollectionViewCell 的大小
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    return CGSizeMake(kScreenWidth / 4, 80);
}
//每个item之间的间距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
{

    return 0;
}

//九宫格
- (void)getHomeEnterListMarket_main
{
    [SVProgressHUD show];
    NSMutableDictionary *params = [[NSMutableDictionary alloc] init];
    [params setObject:@"market_casual" forKey:@"viewType"];
    
    
    
    [iSeeNetworkRequest getWithHeaderUrl:kFormat(@"%@%@",NMarketMainUrl,ISEE_URL_GET_HOMEENTER_LIST) params:params success:^(id object) {
        if ([object[@"code"] integerValue] == 200) {
            if (object[@"body"] && [object[@"body"] isKindOfClass:[NSDictionary class]]) {
                NSDictionary *body = object[@"body"];
                if (body[@"userInfos"] && [body[@"userInfos"] isKindOfClass:[NSArray class]]){
                    NSArray *arr = body[@"userInfos"];
                    for ( NSMutableDictionary *dic in arr) {
                        AppModuleModel *model = [[AppModuleModel alloc] init];
                        model.name = dic[@"name"];
                        model.iosIcon = dic[@"iosIcon"];
                        model.enterUrl = dic[@"enterUrl"];
                        model.enterType = [dic[@"enterType"] integerValue];
                        model.logId = dic[@"logId"];
                        [self.dataList addObject:model];
                    }
                }
            }
            [self.collect reloadData];
        }
        [SVProgressHUD dismiss];
    } failure:^(NSError *error) {
        [SVProgressHUD dismiss];
    }];
    
}
- (void) appModuleModeAction:(AppModuleModel*) itemm
{
    
    if (itemm.enterType == 0)
    {

    }
    else if (itemm.enterType == 1)
    {

    }
    else if(itemm.enterType == 2)
    {
        HCGFReceiptView *webView =[[HCGFReceiptView alloc] init];
        webView.url = itemm.enterUrl;
        
        webView.title = itemm.name;
        [self.navigationController pushViewController:webView animated:YES];
    }
    else if(itemm.enterType == 4)
    {

    } else if(itemm.enterType == 6)
    {

    }
}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
